package com.leo.foodmaster.admin.recipe.controller;

import cn.hutool.core.util.StrUtil;
import com.leo.foodmaster.commons.entity.Result;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import springfox.documentation.annotations.ApiIgnore;
import org.springframework.web.bind.annotation.PostMapping;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.leo.foodmaster.admin.recipe.dto.RecipeTemplateDTO;
import com.leo.foodmaster.admin.recipe.entity.RecipeTemplateEntity;
import com.leo.foodmaster.admin.recipe.service.IRecipeTemplateService;
import javax.annotation.Resource;

/**
 * 菜品模板
 *
 * @author Leo
 * @since 1.0.0 2021-10-29
 */
@RestController
@RequestMapping("/recipeTemplate")
@Api(tags = "菜品模板")
@Slf4j
public class RecipeTemplateController {
    @Resource
    private IRecipeTemplateService irecipeTemplateService;

    @GetMapping("/pageList")
    @ApiOperation("分页")
    public Result<IPage<RecipeTemplateDTO>> pageList(
            @ApiParam(value = "当前页，从1开始", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "每页记录数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "菜品类型Id") @RequestParam(required = false) Long recipeTypeId,
            @ApiParam(value = "菜品名称") @RequestParam(required = false) String name,
            @ApiParam(value = "菜品状态") @RequestParam(required = false) String status
    ){
    	IPage<RecipeTemplateDTO> pageList =
                irecipeTemplateService.pageList(new Page<>(pageNum, pageSize), recipeTypeId, name, status);
        return Result.success(pageList);
    }


    @GetMapping
    @ApiOperation("信息")
    public Result<RecipeTemplateDTO> get(@RequestParam Long id,@ApiIgnore RecipeTemplateDTO recipeTemplateDTO){
    	RecipeTemplateEntity byId = irecipeTemplateService.getById(id);
    	if (BeanUtil.isEmpty(byId)) {
    		return Result.success();
		}
    	BeanUtil.copyProperties(byId, recipeTemplateDTO);
    	return Result.success(recipeTemplateDTO);
    }

    @PostMapping
    @ApiOperation("保存")
    public Result<Boolean> addRecipeTemplate(@RequestBody RecipeTemplateDTO dto){
        Boolean save = irecipeTemplateService.addRecipeTemplate(dto);
        return  Result.success(save);
    }


    @PutMapping
    @ApiOperation("修改")
    public Result<Boolean> update(@RequestBody RecipeTemplateDTO dto){
        Boolean result = irecipeTemplateService.updateRecipeTemplate(dto);
        return  Result.success(result);
    }

    @PutMapping("/updateStatus")
    @ApiOperation("修改菜品模板状态")
    public Result<Boolean> updateStatus(@RequestBody RecipeTemplateDTO dto){
        RecipeTemplateEntity recipeTemplateEntity = new RecipeTemplateEntity();
        BeanUtil.copyProperties(dto, recipeTemplateEntity);
        Boolean result = irecipeTemplateService.updateById(recipeTemplateEntity);
        return  Result.success(result);
    }

    @DeleteMapping("/{ids}")
    @ApiOperation("删除")
    public Result<Object> delete(@ApiParam("菜品模板ids") @PathVariable String ids){
        String[] idArray = ids.split(StrUtil.COMMA);
        List<Long> idList = new ArrayList<>();
        for (String id : idArray) {
            idList.add(Long.parseLong(id));
        }
    	return irecipeTemplateService.deleteBatchIds(idList);
    }

}
